/**
 * 航线查询开发样例. 
 * Copyright 2000-2020 ALPHA LAB.Inc
 * author:allen
 * date:2018-02-22
 */ 
require(["jquery","moonlight.service","moonlight.component.loading","text!./airlineModal.html",
	"airlineEditController.js","qzz.querygrid","layer"],
	function($,moonlight,PageLoading,$modalView,$airlineEditController,$qzz,$layer){ 
	
	/**
	 * 1.定义页面控制器类及其属性
	 */
	var AirlineController = function(){  
		//定义全局变量 
		this.simpleGrid = null;
		this.simpleGrid2 = null;
		this.simpleGrid3 = null;
		this.complexGrid = null;	
		this.complexGrid2 = null;
		//航班编号对航空公司名的ID名称转换Map
		this.flightID2CompNameMap = {};
		//定义常量 
		this.CONST = {
			//航线查询服务
			airlineQueryContext:"com.alphalab.moonlight.demo.service.context.IAirlineQueryContext",
			//航线增删改服务
			airlineContext:"com.alphalab.moonlight.demo.service.context.IAirlineContext",
			//航班查询服务
			airlineFlightQueryContext: "com.alphalab.moonlight.demo.service.context.IAirlineFlightQueryContext",
			//加载层
			pageLoading: new PageLoading()
		}  
	};		
	
	/**
	 * 2.定义方法
	 */ 
	AirlineController.prototype = { 
	    /**
		 * 初始化方法，固定一个init方法包括initPage、bindEvent两个方法
		 */
		init: function() { 
			this.CONST.pageLoading.show();
			this.initPage();
			this.bindEvent();
			//加入模态框html代码
			$("body").append($($modalView));
			//初始化编辑模块的js
			$airlineEditController.init();	
			this.CONST.pageLoading.hide();
		},	
		/**
		 * 初始化页面
		 */
		initPage: function() {
			this.initID2NameMap();
			this.initGrid(); 
		},
		/**
		 * 绑定事件
		 */
		bindEvent: function() {
			//查询按钮的事件响应
			$("#queryBtn").click($.proxy(this.querySimpleGrid, this)); 
			$("#queryBtn2").click($.proxy(this.querySimpleGrid2Total, this));
			$("#queryBtn3").click($.proxy(this.querySimpleGrid3Total, this));
			$("#queryBtn4").click($.proxy(this.queryComplexGrid, this));
			$("#queryBtn5").click($.proxy(this.queryComplexGrid2, this));
			//滚动条滚动的事件相应
			$(window).scroll(this.scrollEvent);
			//重置宽高的事件响应
			$(window).resize($.proxy(this.resize, this));
			//分页事件监听
			this.bindQuerySimpleGrid2ByPage();
			this.bindQuerySimpleGrid3ByPage();
			//表格超链接点击事件监听
			this.bindOpenComplexGrid2Link();
		},
		/**
		 * 初始化ID名称转换Map
		 */
		initID2NameMap: function(){
			var that = this;
			this.flightID2CompNameMap = {};
			var result = moonlight.RemoteService.doPost(this.CONST.airlineFlightQueryContext, 
					"findListWithCondition", [[], "flightId", true]);
			if (result && result.success === true) {
				$.each(result.list, function(index,item){
					that.flightID2CompNameMap[item.flightId] = item.flightCompName+item.flightId;
				});
			} else {
				//弹窗显示错误信息
				this.showErrorDialog(result);
			}
		},
		/**
		 * 获取表格格式
		 */
		getGridStructur: function(){
			var gridStructur = { 
				"height":450,
				"width":$(".grid-table").width(),
				"suitToFit":true,
				"colNames":["航线ID","航班","出发点","终点","出发时间","到达时间","票价"], 
				"colModels":[   
					{"name":"airlineId","hidden":true},        
					{"name":"flightId"}, 
					{"name":"departure"},
					{"name":"destination"},
					{"name":"beginDate","dataType":"date","showTime":true},
					{"name":"endDate","dataType":"date","showTime":true},
					{"name":"price","scale":2,"sum":true, "dataType":"number"}
				]      
			};
			return gridStructur;
		},
		/**
		 * 获取多表查询表格格式
		 */
		getComplexGridStructur: function(){
			var gridStructur = { 
				"height":420,
				"width":$(".grid-table").width(),
				"suitToFit":true,
				"colNames":["乘客证件号","乘客姓名","搭乘航班","目的地","出发时间"],  
				"colModels":[   
					{"name":"idNumber"},    
					{"name":"username"},        
					{"name":"flight"},
					{"name":"destination"},
					{"name":"beginDate","dataType":"date","showTime":true}
				]      
			};
			return gridStructur;
		},		
		/**
		 * 获取返回动态结果集表格格式
		 */
		getComplexGridStructur2: function(){
			var gridStructur = { 
				"treeGrid":true,  
				"height":400,
				"width":$(".grid-table").width(),
		        "idkey":"catalogId",          
		        "pidkey":"pCatalogId",				
				"rownumbers":false,
				"suitToFit":true,
				"footerrow":true,
				"colNames":[
					[{"caption":"3月","startIndex":1,"colSpan":2},{"caption":"4月","startIndex":3,"colSpan":2},
						{"caption":"5月","startIndex":5,"colSpan":2}],
					["航班","台北","上海","台北","上海","台北","上海"]				            
				],   		 
				"colModels":[   
					{"name":"flight"},    
					{"name":"acity_3","sum":true, "dataType":"number","link":true},        
					{"name":"bcity_3","sum":true, "dataType":"number","link":true},
					{"name":"acity_4","sum":true, "dataType":"number","link":true},        
					{"name":"bcity_4","sum":true, "dataType":"number","link":true},
					{"name":"acity_5","sum":true, "dataType":"number","link":true},        
					{"name":"bcity_5","sum":true, "dataType":"number","link":true}					
				]      
			};
			return gridStructur;
		},			
		/**
		 * 初始化查询表格
		 */
		initGrid: function(){
			var gridStructur = this.getGridStructur();
			var gridStructurWithPage = this.getGridStructur(); 
			gridStructurWithPage.pager = true;					
			this.simpleGrid = $("#simpleGrid").qzzquerygrid(gridStructur);
			this.simpleGrid2 = $("#simpleGrid2").qzzquerygrid(gridStructurWithPage);
			this.simpleGrid3 = $("#simpleGrid3").qzzquerygrid(gridStructurWithPage);
			this.complexGrid = $("#complexGrid").qzzquerygrid(this.getComplexGridStructur());
			this.complexGrid2 = $("#complexGrid2").qzzquerygrid(this.getComplexGridStructur2());  
		},
		/**
		 * 查询单表无条件表格
		 */
		querySimpleGrid: function(){
			this.CONST.pageLoading.show();
			var result = moonlight.RemoteService.doPost(this.CONST.airlineQueryContext, 
					"findListWithCondition",[[],"flightId",true]); 
			if (result && result.success === true) {
				this.simpleGrid.value(result.list);
				//航班ID名称转换
				this.simpleGrid.setTransIDName('flightId',this.flightID2CompNameMap);	
				this.simpleGrid.setFocus();
				//光标锁定表格第一行
				this.simpleGrid.first(); 				
			} else {
				//弹窗显示错误信息
				this.showErrorDialog(result);
			}
			this.CONST.pageLoading.hide(); 	 
		},
		/**
		 * 查询单表少条件表格
		 */
		querySimpleGrid2Total: function(){
			this.CONST.pageLoading.show();
			//查询条件集合
			var itemConditions = [];
			//查询条件对象
			var itemCondition = {};
			itemCondition.jcls = "com.alphalab.framework.domain.ItemCondition";
			//属性名
			itemCondition.conditionField="departure";
			//操作符，0代表等于，各数值意义具体查看QueryOperatorEnum枚举类
			itemCondition.operator="0";
			//属性值
			itemCondition.value="深圳";
			var itemCondition2 = {};
			itemCondition2.jcls = "com.alphalab.framework.domain.ItemCondition";
			itemCondition2.conditionField="destination";
			itemCondition2.operator="0";
			itemCondition2.value="台北";			
			itemConditions.push(itemCondition); 
			itemConditions.push(itemCondition2); 
			//后两个参数为排序的字段、是否升序
			var result = moonlight.RemoteService.doPost(this.CONST.airlineQueryContext,"findListWithCondition",
					[itemConditions,"beginDate", true]);
			if (result && result.success === true) {
				//总记录数
				this.simpleGrid2.setTotalRecord(result.list.length);					
			} else {
				//弹窗显示错误信息
				this.showErrorDialog(result);
			}
			this.CONST.pageLoading.hide(); 				
		},
		/**
		 * 查询单表多条件表格
		 */
		querySimpleGrid3Total: function(){
			this.CONST.pageLoading.show(); 
			//查询条件对象
			var queryParamVO = {};
			queryParamVO.jcls = "com.alphalab.moonlight.demo.service.model.FlightQueryParamVO";
			queryParamVO.departure = "深圳";
			queryParamVO.beginTime = "2018-03-01";
			queryParamVO.endTime = "2018-03-11";
			queryParamVO.flights = "CZ3087,MU2519"; 
			var result = moonlight.RemoteService.doPost(this.CONST.airlineQueryContext, "getAirlineList", 
					[queryParamVO]);
			if (result && result.success === true) {
				this.simpleGrid3.setFocus();
				this.simpleGrid3.first(); 	 
				this.simpleGrid3.setTotalRecord(result.list.length);				
			} else {
				//弹窗显示错误信息
				this.showErrorDialog(result);
			}
			this.CONST.pageLoading.hide();
		},
		/**
		 * 查询多表多条件表格
		 */
		queryComplexGrid: function(){
			this.CONST.pageLoading.show();
			var queryParamVO = {};
			queryParamVO.jcls = "com.alphalab.moonlight.demo.service.model.FlightQueryParamVO";
			queryParamVO.departure = "深圳";
			queryParamVO.beginTime = "2018-03-01";
			queryParamVO.endTime = "2018-03-03";			
			var result = moonlight.RemoteService.doPost(this.CONST.airlineQueryContext,"getPassengerTravelList",
					[queryParamVO]);
			if (result && result.success === true) {
				this.complexGrid.value(result.list);
				//航班ID名称转换
				this.complexGrid.setTransIDName('flight',this.flightID2CompNameMap);				
				this.complexGrid.setFocus(); 
				this.complexGrid.first(); 								
			} else {
				//弹窗显示错误信息
				this.showErrorDialog(result);
			}
			this.CONST.pageLoading.hide(); 	 
		},	
		/**
		 * 查询返回动态结果集表格
		 */
		queryComplexGrid2: function(){
			this.CONST.pageLoading.show(); 
			var queryParamVO = {};
			queryParamVO.jcls = "com.alphalab.moonlight.demo.service.model.FlightTrendQueryParamVO";
			queryParamVO.startMonth = 3;
			queryParamVO.endMonth = 5;
			queryParamVO.analyseCity = "台北";	
			queryParamVO.standarCity = "上海";
			var result = moonlight.RemoteService.doPost(this.CONST.airlineQueryContext, 
					"getFlightTrendAnalyseResult", [queryParamVO]);
			if (result && result.success === true) {
				this.complexGrid2.value(result.list);
				this.complexGrid2.setFocus(); 
				this.complexGrid2.first(); 	
			} else {
				//弹窗显示错误信息
				this.showErrorDialog(result);
			}
			this.CONST.pageLoading.hide();
		}, 
		/**
		 * SimpleGrid2分页事件监听
		 */
		bindQuerySimpleGrid2ByPage: function() {
			//查询条件集合
			var itemConditions = [];
			//查询条件对象
			var itemCondition = {};
			itemCondition.jcls = "com.alphalab.framework.domain.ItemCondition";
			//属性名
			itemCondition.conditionField="departure";
			//操作符，0代表等于，各数值意义具体查看QueryOperatorEnum枚举类
			itemCondition.operator="0";
			//属性值
			itemCondition.value="深圳";
			var itemCondition2 = {};
			itemCondition2.jcls = "com.alphalab.framework.domain.ItemCondition";
			itemCondition2.conditionField="destination";
			itemCondition2.operator="0";
			itemCondition2.value="台北";			
			itemConditions.push(itemCondition); 
			itemConditions.push(itemCondition2); 
			var that = this;
			//分页查询
			this.simpleGrid2.bind('onPageChanged',function(pageCount, pageSize, pageIndex){
				that.CONST.pageLoading.show();
				var pageResult = moonlight.RemoteService.doPost(that.CONST.airlineQueryContext,
						"findListByPageWithCondition",[itemConditions,pageIndex,pageSize,"beginDate",true]);
				if (pageResult && pageResult.success === true) {
					that.simpleGrid2.value(pageResult.list); 
				} else {
					//弹窗显示错误信息
					that.showErrorDialog(result);
				}
				that.CONST.pageLoading.hide();
			});
		},
		/**
		 * SimpleGrid3分页事件监听
		 */
		bindQuerySimpleGrid3ByPage: function() {
			//查询条件对象
			var queryParamVO = {};
			queryParamVO.jcls = "com.alphalab.moonlight.demo.service.model.FlightQueryParamVO";
			queryParamVO.departure = "深圳";
			queryParamVO.beginTime = "2018-03-01";
			queryParamVO.endTime = "2018-03-11";
			queryParamVO.flights = "CZ3087,MU2519";
			var that = this;
			//分页查询
			this.simpleGrid3.bind('onPageChanged',function(pageCount, pageSize, pageIndex){
				that.CONST.pageLoading.show(); 
				var pageResult = moonlight.RemoteService.doPost(that.CONST.airlineQueryContext, 
						"getAirlineListByPage", [queryParamVO,pageIndex,pageSize]);	
				if (pageResult && pageResult.success === true) {
					that.simpleGrid3.value(pageResult.list); 
				} else {
					//弹窗显示错误信息
					that.showErrorDialog(result);
				}
				that.CONST.pageLoading.hide(); 	
			});	
		},
		/**
		 * ComplexGrid2超链接事件监听
		 */
		bindOpenComplexGrid2Link: function() {
			var that = this;
			this.complexGrid2.bind('onLinkClick', function(grd, fieldName, node) { 
				if(fieldName.indexOf("acity")>-1||fieldName.indexOf("bcity")>-1){
					layer.alert("选中的值为" + that.complexGrid2.getValue(fieldName));
				}  
			});			
		},
		/**
		 * 鼠标滚动响应事件
		 */
		scrollEvent: function(){
			if($(document).scrollTop()>=100){ 
				$(".bar").addClass("fix");
			}else{
				$(".bar").removeClass("fix");
			}			
		},
		/**
		 * 重置宽高
		 */
		resize: function(){
			//对已初始化的表格重置宽高
			this.simpleGrid.setWidth($(".grid-table").width());
			this.simpleGrid2.setWidth($(".grid-table").width());
			this.simpleGrid3.setWidth($(".grid-table").width());
			this.complexGrid.setWidth($(".grid-table").width());	
			this.complexGrid2.setWidth($(".grid-table").width());
		},
		/**
		 * 异常信息展示
		 */
		showErrorDialog: function(errorObj) {
			if (!errorObj) {
				layer.open({
				    title: '提示',
				    content: '服务器连接失败'
				});
				return;
			}			
			if (!(errorObj.code || errorObj.msg)) {
				layer.open({
				    title: '提示',
				    content: '服务器返回数据类型错误'
				});
				return;
			}
			var layOptions = {
				title: '提示',
				area: '400px',
				content: '错误码：' + errorObj.code + '<br>错误信息：' + errorObj.msg					
			};
			var errStr = errorObj.errorDetail;
			if (errStr && errStr.length > 0) {
				var exceptionStr = errStr.replace(/\n/g,'<br>&emsp;&emsp;');
				layOptions.btn = ['查看详情', '关闭'];
				layOptions.yes = function(index, layero){
				    layer.open({
					    title: '错误详情',
					    area: '920px',
					    content: exceptionStr
				    });
				};
			}
			layer.open(layOptions);			
		}
	}
	
	/**
	 * 3.创建对象
	 */
	var airlineController = new AirlineController();
	
	/**
	 * 4.调用初始化方法init
	 */
	airlineController.init();  
	
});
